package com.tfj.mvp.tfj.per.logreg;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.hyphenate.tfj.R;
import com.jakewharton.rxbinding2.view.RxView;
import com.tfj.comm.bean.Constant;
import com.tfj.mvp.base.BaseActivity;
import com.tfj.utils.AuthPreferences;
import com.tfj.utils.PopupWindowFactory;
import com.tfj.utils.SysUtils;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Hq on 2017/12/21.
 *
 * @author HuangQiang
 * @github https://github.com/HuangQiang
 */

public class VRegisterActivity extends BaseActivity<PRegisterImpl> implements CRegister.IVRegister {


    @BindView(R.id.imagebtn_back)
    ImageButton imagebtnBack;
    @BindView(R.id.editText_logAcc)
    EditText editTextLogAcc;
    @BindView(R.id.editText_logPsd)
    EditText editTextLogPsd;
    @BindView(R.id.editText_yzm)
    EditText editTextYzm;
    @BindView(R.id.btn_getCode)
    Button textviewYzm;
    @BindView(R.id.btn_log)
    Button btnLog;
    @BindView(R.id.textView_top1)
    TextView textViewTop1;
    @BindView(R.id.textView_top2)
    TextView textViewTop2;
    @BindView(R.id.editText_repass)
    EditText editTextRepass;
    @BindView(R.id.ll_agreement)
    LinearLayout llAgreement;
    private PopupWindowFactory mPopupWindow_center;

    @Override
    protected void initView() {
        super.initView();
        boolean ifReg = getIntent().getBooleanExtra("ifReg", true);
        dealType(ifReg);
        View popupView = getLayoutInflater().inflate(R.layout.pop_agreement, null);
        addDisposable(RxView.clicks(llAgreement).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                .subscribe(o -> {
                    mPopupWindow_center.showAtLocation(btnLog, Gravity.CENTER, 0, 0);
                }));
        mPopupWindow_center = new PopupWindowFactory(this, popupView, SysUtils.getScreenWidth(this)-SysUtils.dip2px(this, 32),SysUtils.getScreenHeight(this)- SysUtils.dip2px(this, 176), false, 5);
        addDisposable(RxView.clicks(imagebtnBack).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                .subscribe(o -> {
                    this.finish();
                }));
        addDisposable(RxView.clicks(textviewYzm).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                .subscribe(o -> {
                    String tel = editTextLogAcc.getText().toString().trim();
                    String password = editTextLogPsd.getText().toString().trim();
                    if (TextUtils.isEmpty(tel)) {
                        showToast("手机号不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(password)) {
                        showToast("密码不能为空");
                        return;
                    }
                    loadingView(true, "");
                    mPresenter.getCode(tel);
                }));
        addDisposable(RxView.clicks(btnLog).throttleFirst(Constant.SECONDS_FORBID, TimeUnit.SECONDS)
                .subscribe(o -> {
                    String tel = editTextLogAcc.getText().toString().trim();
                    String password = editTextLogPsd.getText().toString().trim();
                    String yzm = editTextYzm.getText().toString().trim();
                    if (TextUtils.isEmpty(tel)) {
                        showToast("手机号不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(password)) {
                        showToast("密码不能为空");
                        return;
                    }
                    if (TextUtils.isEmpty(yzm)) {
                        showToast("验证码不能为空");
                        return;
                    }
                    loadingView(true, "");
                    mPresenter.regPhone(tel, password, yzm);
                }));


    }

    private void dealType(boolean ifReg) {
        llAgreement.setVisibility(ifReg?View.VISIBLE:View.GONE);
        textViewTop1.setText(ifReg ? "注册" : "更改密码");
        textViewTop2.setText(ifReg ? "注册登陆更便捷" : "请更改您的密码");
        btnLog.setText(ifReg ? "注册" : "确定");

    }

    @Override
    public int setContentViewId() {
        ifSliding = false;
        return R.layout.activity_register;
    }

    @Override
    public void createPresenter() {
        mPresenter = new PRegisterImpl(mContext, this);
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    CountDownTimer timer_reg = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            textviewYzm.setText(millisUntilFinished / 1000 + "秒");
        }

        @Override
        public void onFinish() {
            textviewYzm.setEnabled(true);
            textviewYzm.setText("发送验证码");
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        timer_reg.cancel();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void callBackGetCode(int code, JSONObject jsonObject, String message) {
        loadingView(false, "");
        showToast(message);
        if (code == 1) {
            textviewYzm.setEnabled(false);
            timer_reg.start();
        }
    }

    @Override
    public void callBackReg(int code, JSONObject jsonObject, String message) {
        loadingView(false, "");
        showToast(message);
        if (code == 1) {
            String userSring = jsonObject.toJSONString();
            AuthPreferences.saveUserToken(userSring);
        } else {

        }

    }
}
